/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_RealTime.c
 *
 *  Created on: 2022年9月29日
 *      Author: Casa XU
 */


#include <LOS_RealTime.h>


uint16_t LOS_Time_MilSec = 0;         //系统时钟毫秒值
uint32_t LOS_Time_Sec = 0;            //系统时钟秒值
uint32_t LOS_Time_SecFromBoot = 0;   //系统时钟秒（从启动算起）
double LOS_Time_Double = 0;           //系统时钟，换算成小数
uint16_t LOS_RealTime_Year = 2017;
uint8_t LOS_RealTime_Month = 1;
uint8_t LOS_RealTime_Day = 1;
uint8_t LOS_RealTime_Minute = 0;
uint8_t LOS_RealTime_Hour = 0;
uint8_t LOS_RealTime_Second = 0;

void LOS_RTC_Calculate(uint32_t Second)
{
    uint16_t Year = 2017;
    uint8_t Month = 1;
    uint8_t Day = 1;
    uint32_t DayCnt = 0;
    uint32_t SecRem = 0;
    uint8_t Minute = 0;
    uint8_t Hour = 0;


    DayCnt = Second/86400;
    SecRem = Second%86400;

    while(DayCnt >= 366)
    {
        if ((Year%4)==0)
        {
            DayCnt -= 366;
            Year++;
        }
        else
        {
            DayCnt -= 365;
            Year++;
        }
    }

    if (DayCnt >= 365)
    {
        if ((Year%4)!=0)
        {
            DayCnt -= 365;
            Year++;
        }
    }

    while (DayCnt >= 31)
    {
        if (Month ==1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month ==12)
        {
            DayCnt -= 31;
            Month ++;
        }
        else if (Month == 2)
        {
            if ((Year%4)==0)
            {
                DayCnt -= 29;
            }
            else
            {
                DayCnt -= 28;
            }
            Month ++;
        }
        else
        {
            DayCnt -= 30;
            Month ++;
        }

        if (Month == 13)
        {
            Month = 1;
            Year ++;
        }
    }

    if (DayCnt == 30)
    {
        if (Month ==4 || Month == 6 || Month == 9 || Month == 11)
        {
            Month ++;
        }
    }

    if (DayCnt == 29)
    {
        if (Month == 2 && (Year%4)==0)
        {
            Month ++;
        }
    }


    if (DayCnt == 28)
    {
        if (Month == 2 && (Year%4)!=0)
        {
            Month ++;
        }
    }

    Day += DayCnt;

    Hour = SecRem / 3600;
    Second = SecRem % 3600;
    Minute = Second / 60;
    Second = Second % 60;

    LOS_RealTime_Year = Year;
    LOS_RealTime_Month = Month;
    LOS_RealTime_Day = Day;
    LOS_RealTime_Hour = Hour;
    LOS_RealTime_Minute = Minute;
    LOS_RealTime_Second = Second;
}
